﻿using System.Collections.Generic;
using System.Threading.Tasks;
using GrapeCity.Forguncy.Commands;
using TencentCloud.Common;
using TencentCloud.Ocr.V20181119.Models;
using TencentCloudOcrPlugin.Resources;

namespace TencentCloudOcrPlugin.Ocr
{
    public class OcrVatInvoiceAction : OcrActionBase
    {
        
        public override List<string> GetParamList(TencentCloudApiPropertyBase properties)
        {
            var list = base.GetParamList(properties);
            list.Add("文本信息");
            list.Add("明细条目");
            return list;
        }

        public override async Task<Dictionary<string,object>> RunAsync(TencentCloudApiClientConfig clientConfig,
            IServerCommandExecuteContext dataContext, TencentCloudApiPropertyBase properties)
        {
            var ocrProperties = properties as OcrVatInvoiceProperties;
            var baseRequest = await BuildRequestAsync(ocrProperties, dataContext);
            var request = new VatInvoiceOCRRequest()
            {
                ImageBase64 = baseRequest.ImageBase64,
                ImageUrl = baseRequest.ImageUrl,
                IsPdf = baseRequest.IsPdf
            };
            var response = await GetClient(clientConfig).VatInvoiceOCR(request);
            return BuildResp(response, properties);
        }

        private Dictionary<string, object> BuildResp(AbstractModel tencentResp, TencentCloudApiPropertyBase properties) {
            var dict = new Dictionary<string, object>();
            if (!(tencentResp is VatInvoiceOCRResponse response))
            {
                return dict;
            }
            if (properties.advancedSettings.TencentRawResp)
            {
                dict["原始数据"] = tencentResp;
            }

            var infos = new Dictionary<string, object>();
            foreach (var responseVatInvoiceInfo in response.VatInvoiceInfos)
            {
                infos[responseVatInvoiceInfo.Name] = responseVatInvoiceInfo.Value;
            }
            dict["文本信息"] = infos;
            dict["明细信息"] = response.Items;
            return dict;
        }
    }
}